home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / layout / example / DiagonalLayout.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.9 KB  |  183 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.util.Vector;
  19.  
  20. public class DiagonalLayout implements LayoutManager {
  21.  
  22.     private int vgap;
  23.     private int minWidth = 0, minHeight = 0;
  24.     private int preferredWidth = 0, preferredHeight = 0;
  25.     private boolean sizeUnknown = true;
  26.  
  27.     public DiagonalLayout() {
  28.         this(5);
  29.     }
  30.  
  31.     public DiagonalLayout(int v) {
  32.         vgap = v;
  33.     }
  34.  
  35.     /* Required by LayoutManager. */
  36.     public void addLayoutComponent(String name, Component comp) {
  37.     }
  38.  
  39.     /* Required by LayoutManager. */
  40.     public void removeLayoutComponent(Component comp) {
  41.     }
  42.  
  43.     private void setSizes(Container parent) {
  44.         int nComps = parent.countComponents();
  45.         Dimension d = null;
  46.  
  47.         //Reset preferred/minimum width and height.
  48.         preferredWidth = 0;
  49.         preferredHeight = 0;
  50.         minWidth = 0;
  51.         minHeight = 0;
  52.  
  53.         for (int i = 0; i < nComps; i++) {
  54.             Component c = parent.getComponent(i);
  55.             if (c.isVisible()) {
  56.                 d = c.preferredSize();
  57.  
  58.                 if (i > 0) {
  59.                     preferredWidth += d.width/2; 
  60.                     preferredHeight += vgap;
  61.                 } else {
  62.                     preferredWidth = d.width;
  63.                 }
  64.                 preferredHeight += d.height;
  65.  
  66.                 minWidth = Math.max(c.minimumSize().width, minWidth);
  67.                 minHeight = preferredHeight;
  68.             }
  69.         }
  70.     }
  71.  
  72.  
  73.     /* Required by LayoutManager. */
  74.     public Dimension preferredLayoutSize(Container parent) {
  75.         Dimension dim = new Dimension(0, 0);
  76.         int nComps = parent.countComponents();
  77.  
  78.         setSizes(parent);
  79.  
  80.         //Always add the container's insets!
  81.         Insets insets = parent.insets();
  82.         dim.width = preferredWidth + insets.left + insets.right;
  83.         dim.height = preferredHeight + insets.top + insets.bottom;
  84.  
  85.         sizeUnknown = false;
  86.  
  87.         return dim;
  88.     }
  89.  
  90.     /* Required by LayoutManager. */
  91.     public Dimension minimumLayoutSize(Container parent) {
  92.         Dimension dim = new Dimension(0, 0);
  93.         int nComps = parent.countComponents();
  94.  
  95.         //Always add the container's insets!
  96.         Insets insets = parent.insets();
  97.         dim.width = minWidth + insets.left + insets.right;
  98.         dim.height = minHeight + insets.top + insets.bottom;
  99.  
  100.         sizeUnknown = false;
  101.  
  102.         return dim;
  103.     }
  104.  
  105.     /* Required by LayoutManager. */
  106.     /* This is called when the panel is first displayed, 
  107.      * and every time its size changes. 
  108.      * Note: You CAN'T assume preferredLayoutSize() or minimumLayoutSize()
  109.      * will be called -- in the case of applets, at least, they probably
  110.      * won't be. */
  111.     public void layoutContainer(Container parent) {
  112.         Insets insets = parent.insets();
  113.         int maxWidth = parent.size().width
  114.                        - (insets.left + insets.right);
  115.         int maxHeight = parent.size().height
  116.                         - (insets.top + insets.bottom);
  117.         int nComps = parent.countComponents();
  118.         int previousWidth = 0, previousHeight = 0;
  119.         int x = 0, y = insets.top;
  120.         int rowh = 0, start = 0;
  121.         int xFudge = 0, yFudge = 0;
  122.         boolean oneColumn = false;
  123.  
  124.         // Go through the components' sizes, if neither preferredLayoutSize()
  125.         // nor minimumLayoutSize() has been called.
  126.         if (sizeUnknown) {
  127.             setSizes(parent);
  128.         }
  129.             
  130.         if (maxWidth <= minWidth) {
  131.             oneColumn = true;
  132.         }
  133.  
  134.         if (maxWidth != preferredWidth) {
  135.             xFudge = (maxWidth - preferredWidth)/(nComps - 1);
  136.         }
  137.  
  138.         if (maxHeight > preferredHeight) {
  139.             yFudge = (maxHeight - preferredHeight)/(nComps - 1);
  140.         }
  141.  
  142.         for (int i = 0 ; i < nComps ; i++) {
  143.             Component c = parent.getComponent(i);
  144.             if (c.isVisible()) {
  145.                 Dimension d = c.preferredSize();
  146.                 
  147.                  // increase x and y, if appropriate
  148.                 if (i > 0) { 
  149.                     if (!oneColumn) {
  150.                             //x += previousWidth - d.width/2 + xFudge;
  151.                         x += previousWidth/2 + xFudge;
  152.                     }
  153.                     y += previousHeight + vgap + yFudge;
  154.                 }
  155.                 
  156.                 // If x is too large, ...
  157.                 if ((!oneColumn) &&
  158.                     (x + d.width) > (parent.size().width - insets.right)) {
  159.                     // ... reduce x to a reasonable number.
  160.                     x = parent.size().width - insets.bottom - d.width;
  161.                 }
  162.  
  163.                 // If y is too large, ...
  164.                 if ((y + d.height) > (parent.size().height - insets.bottom)) {
  165.                     // ... do nothing.
  166.                     // Another choice would be to do what we do to x.
  167.                 }
  168.  
  169.                 // Set the component's size and position.
  170.                 c.reshape(x, y, d.width, d.height);
  171.  
  172.                 previousWidth = d.width;
  173.                 previousHeight = d.height;
  174.             }
  175.         }
  176.     }
  177.     
  178.     public String toString() {
  179.         String str = "";
  180.         return getClass().getName() + "[vgap=" + vgap + str + "]";
  181.     }
  182. }
  183.